﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;
using System.Globalization;
using System.Resources;
using EHTWebsite.Code;
using System.Text;
using System.IO;
using System.Net;
using System.Web.Security;
using EHTWebsiteProject.Code.Entity;
using EHTWebsiteProject.Code.BLL;
namespace EHTWebsiteProject.Administrator
{
    public partial class EHTMenusUI : System.Web.UI.Page
    {
        private String message = "";
        private Int32 ID = 0; 
        public Boolean IsVN = true;
        private String languageCode = "vi-VN";
        private ResourceManager rm = new ResourceManager("Resources.SiteMap", System.Reflection.Assembly.Load("App_GlobalResources"));

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                try
                {
                    languageCode = Session[HangSo.LANGUAGE_CODE].ToString();
                    if (languageCode == "en-us")
                    {
                        IsVN = false;
                    }

                }
                catch (Exception ex)
                {
                }

                try
                {
                    ID = Int32.Parse(Request.QueryString["ID"]);
                }
                catch
                { 
                
                }

                try
                {
                    message = Request.QueryString["Message"].ToString();
                    if(message!="")
                        ltrErorr.Text = HelpMethod.ApplySuccess(message);
                }
                catch
                {

                }               
                Thread.CurrentThread.CurrentCulture = new CultureInfo(languageCode);
                LoadLanguage(Thread.CurrentThread.CurrentCulture);

                if (ID != 0)
                {
                    if (IsVN == true)
                        lbAdd_Edit.Text = HangSo.Edit_VN;
                    else
                        lbAdd_Edit.Text = HangSo.Edit_Other;
                    DanhMucMenus theDanhMucMenus = DanhMucMenusProvider.GetDanhMucMenusByID(ID);
                    txtMota.Text = theDanhMucMenus.Description;
                    txtTitleOrder.Text = theDanhMucMenus.TitleOrder;
                    ViewState["action"] = HangSo.ACTION_UPDATE;
                    ViewState["ID"] = ID;
                    ViewState["TypeMenu"] = theDanhMucMenus.TypeMenu;
                }
                else
                {
                    ViewState["action"] = HangSo.ACTION_ADD;
                    if(IsVN==true)
                        lbAdd_Edit.Text = HangSo.Add_VN;
                    else
                        lbAdd_Edit.Text = HangSo.Add_Other;
                }

            }
        }

        /// <summary>
        /// Ham load ngon ngu
        /// </summary>
        /// <param name="ci"></param>
        public void LoadLanguage(CultureInfo ci)
        {
            lbSave.Text = rm.GetString("lbSave", ci);
            lbCancel.Text = rm.GetString("lbCancel", ci);
            lbApply.Text = rm.GetString("lbApply", ci);
            lbHelp.Text = rm.GetString("lbHelp", ci);
            lbMota.Text = rm.GetString("lbMota", ci);
            lbName.Text = rm.GetString("lbName", ci);


        }

        /// <summary>
        /// Ham kiem tra du lieu
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public Boolean ValidateData()
        {
            Boolean flag = true;

            if (txtTitleOrder.Text == "")
            {
                if (IsVN == true)
                    message = HangSo.check_VN;
                else
                    message = HangSo.check_Other;
                flag =  false;
            }
            return flag;
        }

        protected void ImgSave_Click(object sender, ImageClickEventArgs e)
        {
            String action = ViewState["action"].ToString();
            Int32 MenuIDOut = -1;
            if (ValidateData() == false)
            {
                ltrErorr.Text = HelpMethod.ErorrLogin(message);
                return;
            }
            else
            {
                DanhMucMenus theDanhMucMenus = new DanhMucMenus();
                theDanhMucMenus.TitleOrder = txtTitleOrder.Text.Trim();
                theDanhMucMenus.Description = txtMota.Text.Trim();
                                          
                if (action == HangSo.ACTION_ADD)
                {
                    theDanhMucMenus.TypeMenu = 2;
                    theDanhMucMenus.ID = -1;
                    MenuIDOut = DanhMucMenusProvider.UIDanhMucMenus(theDanhMucMenus, true);
                }
                else
                {
                    theDanhMucMenus.TypeMenu = Int32.Parse(ViewState["TypeMenu"].ToString());
                    theDanhMucMenus.ID = Int32.Parse(ViewState["ID"].ToString());
                    MenuIDOut = DanhMucMenusProvider.UIDanhMucMenus(theDanhMucMenus, false);
                }

                Response.Redirect("EHTMenus.aspx");
            }
        }

        protected void ImgApply_Click(object sender, ImageClickEventArgs e)
        {
            String action = ViewState["action"].ToString();
            Int32 MenuIDOut = -1;

            if (ValidateData() == false)
            {
                ltrErorr.Text = HelpMethod.ErorrLogin(message);
                return;
            }
            else
            {
                DanhMucMenus theDanhMucMenus = new DanhMucMenus();
                theDanhMucMenus.TitleOrder = txtTitleOrder.Text.Trim();
                theDanhMucMenus.Description = txtMota.Text.Trim();             

                if (action == HangSo.ACTION_ADD)
                {
                    theDanhMucMenus.TypeMenu = 2;
                    theDanhMucMenus.ID = -1;
                    MenuIDOut = DanhMucMenusProvider.UIDanhMucMenus(theDanhMucMenus, true);
                }
                else
                {
                    theDanhMucMenus.TypeMenu = Int32.Parse(ViewState["TypeMenu"].ToString());
                    theDanhMucMenus.ID = Int32.Parse(ViewState["ID"].ToString());
                    MenuIDOut = DanhMucMenusProvider.UIDanhMucMenus(theDanhMucMenus, false);
                    
                }
                String str = "";
                if (IsVN == true)
                    str = HangSo.App_VN;
                else
                    str = HangSo.App_Other;
                Response.Redirect("EHTMenusUI.aspx?ID=" + ID.ToString() + "&&Message="+str);
            }
        }
    }
}